import path from 'path';

import db, {where} from '../../data/db';
const TABLE = path.basename(__dirname);//默认表名称为当前文件夹名称
const model = db.get(TABLE);
const columns = 'Id,Name,NameJp,Type,Comment,Status,SpecialDepartmentId,DepartmentId,UserId,IsDef'.split(',');


export function get(query = {}) {
    const {pageSize = 0, page = 0} = query;
    let _fs = query._fs;
    if (_fs) {
        if (_fs !== '*') {
            _fs = _fs.split(',');
        }
    } else {
        _fs = columns;
    }
    const ps = where(model, query);
    return pageSize && page
        ? ps.fetchPage({pageSize, page, columns: _fs})
        .then(result =>
            ps.count().then(
                count => ({
                    count,
                    data: result
                }))
        )
        : ps.fetchAll({columns: _fs});
}

export function save(data) {
    return new model(data).save();
}

export function del(id) {
    return model.forge({Id: id}).destroy();
}
